package com.ficus.road.maintain.service.carmanager;

import com.ficus.road.maintain.core.model.carmanager.CarInfo;
import com.ficus.road.maintain.dao.carmanager.CarInfoDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author xiawen yang
 * @date 2022/2/18 下午3:02
 */
@Service
public class CarInfoService {
    @Autowired
    private CarInfoDao carInfoDao;

    public void saveOrUpdate(CarInfo carInfo) {
        carInfoDao.saveOrUpdate(carInfo);
    }

    public CarInfo findByCarId(String carId) {
        Query query = new Query(Criteria.where("car_id").is(carId));
        return carInfoDao.findOne(query);
    }

    public List<CarInfo> findAll() {
        return carInfoDao.findAll();
    }

    public void modifyCarInfoByDefaultUserId(String carId, String userId) {
        Query query = new Query(Criteria.where("car_id").is(carId));
        CarInfo one = carInfoDao.findOne(query);
        one.setDefaultUserId(userId);
        carInfoDao.saveOrUpdate(one);
    }
}
